package com.itany.netclass.service.proxy;

import com.itany.mvc.util.CommonsMultipartFile;
import com.itany.netclass.entity.Course;
import com.itany.netclass.entity.CourseType;
import com.itany.netclass.entity.User;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.CourseService;
import com.itany.netclass.tran.TransactionManager;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:21-10-14 下午6:36
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class CourseServiceProxy implements CourseService {
    @Override
    public List<CourseType> findAllType(User user) throws ServiceException, NoLoginException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            List<CourseType> type = courseService.findAllType(user);
            tran.commit();
            return type;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NoLoginException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<Course> findAllCourse() throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            List<Course> course = courseService.findAllCourse();
            tran.commit();
            return course;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaCourseName(String courseId,String courseName, String courseType) throws NullSringException, ServiceException, CourseExistException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            courseService.jianchaCourseName(courseId,courseName, courseType);
            tran.commit();
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (CourseExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void jianchaCourseInfo(String courseInfo) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            courseService.jianchaCourseInfo(courseInfo);
            tran.commit();
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaCourseAuthor(String courseAuthor) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            courseService.jianchaCourseAuthor(courseAuthor);
            tran.commit();
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaCourseTuijian(String courseTuijian) throws NullSringException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            courseService.jianchaCourseTuijian(courseTuijian);
            tran.commit();
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaCourseType(String courseId,String courseType, String courseName) throws ServiceException, NullSringException, CourseExistException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            courseService.jianchaCourseType(courseId,courseType, courseName);
            tran.commit();
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (CourseExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void jianchaCourseFile(String courseId, HttpSession session, List<CommonsMultipartFile> list) throws ServiceException, NullSringException, TypeExistException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            courseService.jianchaCourseFile(courseId, session, list);
            tran.commit();
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (TypeExistException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void addCourse(String courseId, String courseName, String courseInfo, String author, String tuijian, String type, List<CommonsMultipartFile> list, HttpSession session) throws Exception {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            courseService.addCourse(courseId, courseName, courseInfo, author, tuijian, type, list, session);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (Exception e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public Course findCourseById(String id) throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            Course course = courseService.findCourseById(id);
            tran.commit();
            return course;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void modifyCourseById(String courseId, String courseName, String courseInfo, String author, String tuijian, String type, HttpSession session, List<CommonsMultipartFile> list) throws Exception {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            courseService.modifyCourseById(courseId,courseName,courseInfo,author,tuijian,type,session,list);
            tran.commit();
        } catch (Exception e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void yongCourseById(String courseId, String status) throws Exception {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            courseService.yongCourseById(courseId,status);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();;
            throw e;
        } catch (NoQuanXianException e) {
            tran.rollback();;
            throw e;
        }
    }

    @Override
    public List<Course> sreachCourseByForm(String author, String courseName, String status, String type, String kaishitime, String jieshutime) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
        try {
            tran.begin();
            List<Course> courses = courseService.sreachCourseByForm(author, courseName, status, type, kaishitime, jieshutime);
            tran.commit();
            return courses;
        } catch (ServiceException e) {
            tran.commit();
            throw e;
        } catch (NullSringException e) {
            tran.commit();
            throw e;
        }
    }
}